//
//  JobTool.m
//  植物管理
//
//  Created by liujianlong on 2022/11/23.
//

#import "JobTool.h"

@implementation JobTool
+ (void)find:(nullable void(^) (NSMutableArray <Job*>*result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account)];
    [self find:where complete:block];
}

+ (void)update:(Job *)model complete:(nullable void(^) (BOOL success))block{
    [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [NotifyTool post:notifyJobUpdate];
            if (block) {
                block(YES);
            }
        });
    }];
}
#pragma mark - 删除
+ (void)delete:(Job*)model complete:(nullable void(^) (BOOL success))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"stamp"),bg_sqlValue(@(model.stamp)),
                       bg_sqlKey(@"plant"),bg_sqlValue(model.plant)];
    [Job bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [NotifyTool post:notifyJobUpdate];
            if (block) {
                block(isSuccess);
            }
        });
    }];
}

+ (void)find:(NSString *)where complete:(nullable void(^) (NSMutableArray <Job*>*result))block{
    [Job bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block([NSMutableArray arrayWithArray:array]);
            }
        });
    }];
}
@end
